/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.lcm.metadata.core.exception;

import com.inspur.edp.lcm.metadata.api.entity.uri.AbstractURI;
import com.inspur.edp.lcm.metadata.api.entity.uri.MetadataURI;
import com.inspur.edp.lcm.metadata.api.exception.MetadataExceptionCode;
import com.inspur.edp.lcm.metadata.api.exception.MetadataNotFoundException;
import org.springframework.util.StringUtils;

public class MetadataExceptionHandler {

    public static void handleMetadataNotFoundException(MetadataURI targetURI, AbstractURI sourceURI, String projectPath) {
        throw new MetadataNotFoundException("Lcm", MetadataExceptionCode.METADATA_NOT_FOUND, handleMetadataNotFoundMsg(targetURI, sourceURI, projectPath), null, targetURI, sourceURI, projectPath);
    }

    // TODO 增加原因排查
    public static String handleMetadataNotFoundMsg(MetadataURI targetURI, AbstractURI sourceURI, String projectPath) {
        // 找不到表单元数据：63237459-205a-4dfa-832e-bf583e5c03b2，code为s，name为s，nameSpace为Inspur.GS.SysPub.Test.MyTest。
        // 引用位置：在id为e9834564-b569-4604-8ab2-2d0757931f5c，code为s_frm，name为s_frm，nameSpace为Inspur.GS.SysPub.Test.MyTest的元数据中的module字段里。
        // 解决方案请参考：https://igix.inspures.com/app/cms/web/qa/home/pindex.html#/Contents/51e6cf15-bc1f-453f-bfde-95060b774d8b。
        StringBuilder sb = new StringBuilder("未加载到元数据\"");
        sb.append(targetURI.getURIDesc());
        sb.append("\"，");
        if (sourceURI != null && StringUtils.hasLength(sourceURI.getURIDesc())) {
            sb.append("<br>使用方式\"");
            sb.append(sourceURI.getURIDesc());
            sb.append("\"，");
        }

        sb.append("<br>请参考线上<a href='https://igix.inspures.com/app/cms/web/qa/home/pindex.html#/Contents/a55eb0ec-48ae-4951-8b30-0df4a66dc4ed' target='blank'>解决方案</a>。");
        return sb.toString();
    }
}
